package top.yokey.shopnc.activity.seller;

import android.support.v7.widget.AppCompatRadioButton;
import android.support.v7.widget.AppCompatTextView;
import android.support.v7.widget.LinearLayoutCompat;
import android.support.v7.widget.Toolbar;
import android.text.TextUtils;
import android.view.View;

import org.xutils.view.annotation.ContentView;
import org.xutils.view.annotation.ViewInject;

import top.yokey.base.base.BaseHttpListener;
import top.yokey.base.base.BaseSnackBar;
import top.yokey.base.base.BaseToast;
import top.yokey.base.bean.BaseBean;
import top.yokey.shopnc.R;
import top.yokey.shopnc.base.BaseApplication;
import top.yokey.shopnc.base.BaseConstant;
import top.yokey.shopnc.base.SwipeActivity;
import top.yokey.base.model.SellerOrderModel;

/**
 * @author MapStory
 * @ qq 1002285057
 * @ Project https://gitee.com/MapStory/ShopNc-Android
 */

@ContentView(R.layout.activity_seller_order_cancel)
public class OrderCancelActivity extends SwipeActivity {

    @ViewInject(R.id.mainLinearLayout)
    private LinearLayoutCompat mainLinearLayout;
    @ViewInject(R.id.mainToolbar)
    private Toolbar mainToolbar;

    @ViewInject(R.id.moneyTextView)
    private AppCompatTextView moneyTextView;
    @ViewInject(R.id.snTextView)
    private AppCompatTextView snTextView;

    @ViewInject(R.id.oneRadioButton)
    private AppCompatRadioButton oneRadioButton;
    @ViewInject(R.id.twoRadioButton)
    private AppCompatRadioButton twoRadioButton;
    @ViewInject(R.id.thrRadioButton)
    private AppCompatRadioButton thrRadioButton;
    @ViewInject(R.id.fouRadioButton)
    private AppCompatRadioButton fouRadioButton;
    @ViewInject(R.id.submitTextView)
    private AppCompatTextView submitTextView;

    private String orderId;
    private String reason;

    @Override
    public void initData() {

        orderId = getIntent().getStringExtra(BaseConstant.DATA_ID);
        if (TextUtils.isEmpty(orderId)) {
            BaseToast.get().showDataError();
            BaseApplication.get().finish(getActivity());
        }

        setToolbar(mainToolbar, "取消订单");

        reason = "无法备齐货物";
        moneyTextView.setText("￥");
        moneyTextView.append(getIntent().getStringExtra(BaseConstant.DATA_CONTENT));
        snTextView.setText("订单编号：");
        snTextView.append(getIntent().getStringExtra(BaseConstant.DATA_SN));

    }

    @Override
    public void initEven() {

        oneRadioButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                reason = "无法备齐货物";
                oneRadioButton.setChecked(true);
                twoRadioButton.setChecked(false);
                thrRadioButton.setChecked(false);
                fouRadioButton.setChecked(false);
            }
        });

        twoRadioButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                reason = "不是有效的订单";
                oneRadioButton.setChecked(false);
                twoRadioButton.setChecked(true);
                thrRadioButton.setChecked(false);
                fouRadioButton.setChecked(false);
            }
        });

        thrRadioButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                reason = "卖家主动要求";
                oneRadioButton.setChecked(false);
                twoRadioButton.setChecked(false);
                thrRadioButton.setChecked(true);
                fouRadioButton.setChecked(false);
            }
        });

        fouRadioButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                reason = "其他原因";
                oneRadioButton.setChecked(false);
                twoRadioButton.setChecked(false);
                thrRadioButton.setChecked(false);
                fouRadioButton.setChecked(true);
            }
        });

        submitTextView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                cancel();
            }
        });

    }

    //自定义方法

    private void cancel() {

        submitTextView.setEnabled(false);
        submitTextView.setText("取消中...");

        SellerOrderModel.get().orderCancel(orderId, reason, new BaseHttpListener() {
            @Override
            public void onSuccess(BaseBean baseBean) {
                BaseToast.get().show("取消成功");
                BaseApplication.get().finish(getActivity());
            }

            @Override
            public void onFailure(String reason) {
                BaseSnackBar.get().show(mainLinearLayout, reason);
                submitTextView.setEnabled(true);
                submitTextView.setText("提交");
            }
        });

    }

}
